<template>
  <div class="cover">
    <transition :name="transitionName">
      <keep-alive>
        <router-view class="child-view" v-if="$route.meta.keepAlive"></router-view>
      </keep-alive>
    </transition>
    <transition :name="transitionName">
      <router-view class="child-view" v-if="!$route.meta.keepAlive"></router-view>
    </transition>
  </div>
</template>

<script>
  export default {
    name: 'App',
    data(){
      return {
        transitionName: 'slide-right'
      }
    },
    beforeRouteUpdate (to, from, next) {
      // 路由切换滚动位置重置
      document.body.scrollTop = 0;
      let isBack = this.$router.isBack;
      if (isBack) {
        this.transitionName = 'slide-right'
      } else {
        this.transitionName = 'slide-left'
      }
      // 做完回退动画后，要设置成前进动画，否则下次打开页面动画将还是回退
      this.$router.isBack = false;
      next()
    }
  }
</script>

<style lang="less">
  .child-view {
    position: absolute;
    width: 750px;
    min-height: 100%;
    transition: all .4s ease;
  }
  .slide-left-enter, .slide-right-leave-active {
    opacity: 0;
    -webkit-transform: translate(100%, 0);
    transform: translate(100%, 0);
  }
  .slide-left-leave-active, .slide-right-enter {
    opacity: 0;
    -webkit-transform: translate(-100%, 0);
    transform: translate(-100%, 0);
  }

</style>
